-- CALC_AVALON_LAB_BONUS

-- 计算阿瓦隆实验室的奖励
return function(labType)
    local arr = {};
    local rand1, rand2, rand3;
    local arr1,  arr2,  arr3;
    local knowledgeId, ratio1, ratio2;

    -- 根据实验室类型获取奖励
    if labType == "gray_dwarf" then
        -- 灰矮人实验室

        -- 40%概率：随机提升能力 魔力+2或魔法值+30
        -- 30%概率：随机提升【爆破学】能够学习的项目1级；若没有可学项，则不出现
        -- 20%概率：给予【灰矮人爆雷M32 ×3
        -- 10%概率：给予【灰矮人爆雷M48】×2
        arr1 = {{2, "magic", 2}, {2, "max_mp", 30}};
        rand1 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");

        -- 获取爆破学能够学习项目
        -- 必须拥有工程学手册，且有可学项目
        arr2 = EngineeringManualM.getSubjectAllCanLearn(SUBJECT_TYPE_BLASTING, true, true);
        ratio1 = 0;
        if #arr2 > 0 and ItemM.getAmount(ME.user, 7509) > 0 then
            rand2 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");
            knowledgeId = FormulaM.invoke("FETCH_BY_RAND", arr2, rand2);
            ratio1 = 300;
        end

        arr = {
            {["bonus"]  = FormulaM.invoke("FETCH_BY_RAND", arr1, rand1),    ["ratio"] = 400,   },
            {["bonus"]  = {"manual", knowledgeId, 1},                       ["ratio"] = ratio1,},
            {["bonus"]  = {1, 7529, 3},                                     ["ratio"] = 200,   },
            {["bonus"]  = {1, 7530, 2},                                     ["ratio"] = 100,   },
        };
    elseif labType == "dwarf" then
        -- 矮人实验室

        -- 50%概率：随机提升能力：攻击+2或生命值+30
        -- 35%概率：随机提升【冶金学】能够学习的项目1级；若没有可学项，则不出现
        -- 15%概率：随机给予【电击戒指、防弹斗篷、机械挂坠】之一
        arr1 = {{2, "attack", 2}, {2, "max_hp", 30}};
        rand1 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");

        -- 获取冶金学能够学习项目
        -- 必须拥有工程学手册，且有可学项目
        arr2 = EngineeringManualM.getSubjectAllCanLearn(SUBJECT_TYPE_METALLURGY, true, true);
        ratio1 = 0;
        if #arr2 > 0 and ItemM.getAmount(ME.user, 7509) > 0 then
            rand2 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");
            knowledgeId = FormulaM.invoke("FETCH_BY_RAND", arr2, rand2);
            ratio1 = 350;
        end

        -- 获得装备，都获得过则不出现
        arr3 = {8230, 8231, 8232};
        -- 过滤掉已经获得过的装备
        local equip;
        for i = #arr3, 1, -1 do
            if DungeonServiceM.isStatItemLimit(arr3[i]) then
                table.remove(arr3, i);
            end
        end

        ratio2 = 0;
        if #arr3 > 0 then
            rand3 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");
            equip = FormulaM.invoke("FETCH_BY_RAND", arr3, rand3);
            ratio2 = 150;
        end

        arr = {
            {["bonus"]  = FormulaM.invoke("FETCH_BY_RAND", arr1, rand1),    ["ratio"] = 500,   },
            {["bonus"]  = {"manual", knowledgeId, 1},                       ["ratio"] = ratio1,},
            {["bonus"]  = {1, equip, 1},                                    ["ratio"] = ratio2,},
        };
    elseif labType == "goblin" then
        -- 地精实验室

        -- 40%概率：随机给予【地精化合物 I 型】1瓶
        -- 30%概率：随机提升【化学】能够学习的项目1级；若没有可学项，则不出现
        -- 20%概率：随机给予【地精化合物 II 型】1瓶
        -- 10%概率：随机给予【地精化合物 III 型】1瓶
        arr1 = {7531, 7532};
        rand1 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");

        -- 获取化学能够学习项目
        -- 必须拥有工程学手册，且有可学项目
        arr2 = EngineeringManualM.getSubjectAllCanLearn(SUBJECT_TYPE_CHEMISTRY, true, true);
        ratio1 = 0;
        if #arr2 > 0 and ItemM.getAmount(ME.user, 7509) > 0 then
            rand2 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");
            knowledgeId = FormulaM.invoke("FETCH_BY_RAND", arr2, rand2);
            ratio1 = 300;
        end

        arr3 = {7533, 7534};
        rand3 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");

        local arr4 = {7535, 7536};
        local rand4 = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");

        arr = {
            {["bonus"]  = {1, FormulaM.invoke("FETCH_BY_RAND", arr1, rand1), 1},    ["ratio"] = 400,   },
            {["bonus"]  = {"manual", knowledgeId, 1},                               ["ratio"] = ratio1,},
            {["bonus"]  = {1, FormulaM.invoke("FETCH_BY_RAND", arr3, rand3), 1},    ["ratio"] = 200,   },
            {["bonus"]  = {1, FormulaM.invoke("FETCH_BY_RAND", arr4, rand4), 1},    ["ratio"] = 100,   },
        };
    else
        trace("CALC_AVALON_LAB_NEED", "未配置的实验室类型：%s", labType);
        return {};
    end

    local seed = DungeonM.getRandSeed("CALC_AVALON_LAB_BONUS");
    local info = fetchElemBySeed(arr, seed);

    return info["bonus"];
end